package com.ruix.app.manager;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.CountDownTimer;
import android.widget.VideoView;

//import com.danikula.videocache.CacheListener;
//import com.danikula.videocache.HttpProxyCacheServer;
import com.ruix.app.RuiApplication;
import com.ruix.app.util.LogUitl;

import java.io.File;
import java.util.ArrayList;


/**
 * Created by hadexs on 2017/5/12.
 */

public class MediaPlayerManager  {

    private static MediaPlayerManager mediaPlayerManager;
    private MediaPlayer mediaPlayer;
    private ArrayList<MediaPlayerLisenter> playerLisenters;
    private CountDownTimer countDownTimer;

//    private MediaPlayerLisenter playerLisenter;

    public MediaPlayerManager() {
    }


    public synchronized void init() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                if (playerLisenters != null){
                    for (MediaPlayerLisenter lisenter:playerLisenters) {
                        if (lisenter != null){
                            lisenter.onSeekComplete(mp);
                        }
                    }
                }

            }
        });

        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });

        mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });

        mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

            }
        });

        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                if (playerLisenters != null){
                    for (MediaPlayerLisenter lisenter:playerLisenters) {
                        if (lisenter != null){
                            lisenter.onBufferingUpdate(mp,percent);
                        }
                    }
                }
            }
        });
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (playerLisenters != null){
                    for (MediaPlayerLisenter lisenter:playerLisenters) {
                        if (lisenter != null){
                            lisenter.onPrepared(mp);
                        }
                    }
                }
            }
        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (playerLisenters != null){
                    for (MediaPlayerLisenter lisenter:playerLisenters) {
                        if (lisenter != null){
                            lisenter.onCompletion(mp);
                        }
                    }
                }
            }
        });


//        countDownTimer.set



//        mediaPlayer.setDataSource();
//        mediaPlayer.prepareAsync();
//
//
//
//
//
//
//        mediaPlayer.seekTo(11);
//        mediaPlayer.setOnCompletionListener(null);
    }




    public static MediaPlayerManager sharedInstance() {
        if (mediaPlayerManager == null) {
            synchronized (MediaPlayerManager.class) {
                if (mediaPlayerManager == null) {
                    mediaPlayerManager = new MediaPlayerManager();
                    mediaPlayerManager.init();
                }
            }
        }
        return mediaPlayerManager;
    }

    public void playUrl(String videoUrl)
    {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(RuiApplication.getAppContext(), Uri.parse(videoUrl));
            mediaPlayer.prepare();//prepare之后自动播放
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    public void prepareCacheUrl(String videoUrl){
//        try {
//            HttpProxyCacheServer proxy = RuiApplication.getProxy(RuiApplication.getAppContext());
//            proxy.registerCacheListener(this, videoUrl);
//            String proxyUrl = proxy.getProxyUrl(videoUrl);
//            mediaPlayer.reset();
//            mediaPlayer.setDataSource(proxyUrl);
//            mediaPlayer.prepare();//prepare之后自动播放
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
////        mediaPlayer.
//    }


    public void prepareUrl(String videoUrl){
        try {


            mediaPlayer.reset();
            mediaPlayer.setDataSource(RuiApplication.getAppContext(), Uri.parse(videoUrl));
            mediaPlayer.prepare();//prepare之后自动播放
        } catch (Exception e) {
            e.printStackTrace();
        }
//        mediaPlayer.
    }

    public void prepare(){
        try {
            mediaPlayer.prepare();//prepare之后自动播放
        } catch (Exception e) {
            e.printStackTrace();
        }
//        mediaPlayer.
    }

    public void start(){
        if (mediaPlayer != null) {
            try {
                mediaPlayer.start();
                startTimer();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.stop();
                stopTimer();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void pause() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.pause();
                stopTimer();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void seekTo(int msec){
        if (mediaPlayer != null) {
            try {
                mediaPlayer.seekTo(msec);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void reset(){
        if (mediaPlayer != null) {
            try {
                mediaPlayer.reset();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void release(){
        if (mediaPlayer != null) {
            try {
                mediaPlayer.release();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        if (playerLisenters != null){
            playerLisenters.clear();
        }

        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }

    }

    public int getDuration(){
        if (mediaPlayer != null) {
            try {
                return mediaPlayer.getDuration();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  -1;
    }

    public int getCurrentPosition(){
        if (mediaPlayer != null) {
            try {
                return mediaPlayer.getCurrentPosition();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  -1;
    }

    public boolean isPlaying(){
        if (mediaPlayer != null) {
            try {
                return mediaPlayer.isPlaying();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    public void addLisenter(MediaPlayerLisenter lisenter){
        if (playerLisenters == null){
            playerLisenters = new ArrayList<>();
        }
        playerLisenters.add(lisenter);
    }

    public void removeLisenter(MediaPlayerLisenter lisenter){
        if (playerLisenters != null){
            playerLisenters.remove(lisenter);
        }
    }

    public void buildTimer(){
        if (countDownTimer == null) {
            countDownTimer = new CountDownTimer(Integer.MAX_VALUE, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (playerLisenters != null) {
                        for (MediaPlayerLisenter lisenter : playerLisenters) {
                            if (lisenter != null) {
                                lisenter.onPlayerTick();
                            }
                        }
                    }
                }

                @Override
                public void onFinish() {
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                        countDownTimer = null;
                    }
                }
            };
        }
    }

    private void stopTimer(){
        if (countDownTimer != null){
            countDownTimer.cancel();
        }
    }

    private void startTimer(){
        buildTimer();
        if (countDownTimer != null){
            countDownTimer.start();
        }
    }

//    @Override
//    public void onCacheAvailable(File cacheFile, String url, int percentsAvailable) {
//        if (playerLisenters != null) {
//            for (MediaPlayerLisenter lisenter : playerLisenters) {
//                if (lisenter != null) {
//                    lisenter.onCacheAvailable(cacheFile,url, percentsAvailable);
//                }
//            }
//        }
//    }


    public interface  MediaPlayerLisenter{
        void onSeekComplete(MediaPlayer mp);
        void onBufferingUpdate(MediaPlayer mp, int percent);
        void onPrepared(MediaPlayer mp);
        void onCompletion(MediaPlayer mp);
        void onPlayerTick();
        void onCacheAvailable(File cacheFile, String url, int percentsAvailable);
    }




}
